package service.util;

import static org.junit.Assert.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import model.Datentraeger;
import model.Film;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;

import service.impl.DatentraegerRepository;

/**
 * Test case showing how to use the basic {@link GenericDaoFactory}
 * 
 * @author Andre
 */
public class BasicFactorySetup {

	private static final EntityManagerFactory factory = Persistence
			.createEntityManagerFactory("filmverwaltung");

	private DatentraegerRepository datentraegerRepository;
	private EntityManager em;

	private Datentraeger datentraeger;
	private Film film;

	/**
	 * Creates a {@link DatentraegerRepository} instance.
	 * 
	 * @throws Exception
	 */
	@Before
	public void setUp() {

		em = factory.createEntityManager();

		// filmRepository = new JpaRepositoryFactory(em)
		// .getRepository(FilmRepository.class);
		datentraegerRepository = new JpaRepositoryFactory(em)
				.getRepository(DatentraegerRepository.class);

		em.getTransaction().begin();

		datentraeger = new Datentraeger("Datentraeger");
		datentraeger.setName("Datentraeger");

		film = new Film("Film");
		film.setName("Film");
		film.setHauptdarsteller("Hauptdarsteller");
		film.setGenre("Genre");
		film.setLaenge(110);

		datentraeger = datentraegerRepository.save(datentraeger);

	}

	/**
	 * Rollback transaction.
	 */
	@After
	public void tearDown() {

		em.getTransaction().rollback();
	}

	/**
	 * Showing invocation of finder method.
	 */
	@Test
	public void executingFinders() {

		assertEquals(datentraeger,
				datentraegerRepository.findByName("Datentraeger").get(0));
	}
}
